from django.forms import forms
from django.http.response import HttpResponse
from django.utils import timezone
from django.urls import reverse_lazy
from django.views.generic import View, CreateView, ListView, DeleteView
from DjangoMall.views import BaseView
from dadmin.views import HasLoginRequired
from order.models import DmallOrderInfo, DmallShopingCart
from order.forms import DmallOrderInfoForm
from product.models import product


# class DmallBuyNowView(HasLoginRequired, BaseView, CreateView):
#     """ 立即购买视图 """
#     model = DmallOrderInfo
#     # fields = ('order_sn', 'pay_method', 'order_mark', 'address')
#     form_class = DmallOrderInfoForm
#     template_name = "order/buynow.html"
#     success_url = reverse_lazy('/')

#     def form_valid(self, form):
#         form.instance.owner = self.request.user
#         # 生成订单号
#         order_sn = timezone.now().strftime('%Y%m%d%H%M%S') + '%09d' % self.request.user.id
#         form.instance.order_sn = order_sn
#         return super().form_valid(form)


class DmallBuyNowView(HasLoginRequired, CreateView):

    model = DmallShopingCart
    fields = ['spu', 'sku', 'num']
    template_name = 'order/buy.html'

    def form_valid(self, form):
        form.instance.owner = self.request.user
        form.instance.num = self.request.POST['num']
        print(self.request.POST['num'])
        return super().form_valid(form)